﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class QToolDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.QTool.GetAll);
		}

		public static DataTable GetOne(string qToolID)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@QToolID", SqlDbType.NVarChar, qToolID)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.QTool.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static QTool LoadOne(string qToolID)
		{
			QTool o = new QTool();
			DataTable dt = GetOne(qToolID);
			if(dt.Rows.Count > 0)
			{
				o.QToolID = dt.Rows[0][ToolConstants.Entities.QTool.FieldName.QToolID].ToString();
				o.MakerID = dt.Rows[0][ToolConstants.Entities.QTool.FieldName.MakerID].ToString();
				o.MakerName = dt.Rows[0][ToolConstants.Entities.QTool.FieldName.MakerName].ToString();
				o.RRIName = dt.Rows[0][ToolConstants.Entities.QTool.FieldName.RRIName].ToString();
				o.Price = int.Parse(dt.Rows[0][ToolConstants.Entities.QTool.FieldName.Price].ToString());
				o.FrequencyID = dt.Rows[0][ToolConstants.Entities.QTool.FieldName.FrequencyID].ToString();
				o.ToolGroupID = dt.Rows[0][ToolConstants.Entities.QTool.FieldName.ToolGroupID].ToString();
				o.SafeQty = int.Parse(dt.Rows[0][ToolConstants.Entities.QTool.FieldName.SafeQty].ToString());

			}

			return o;
		}


		public static int Insert(QTool o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@QToolID", SqlDbType.NVarChar, o.QToolID),
                        CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                        CreateParameter("@MakerName", SqlDbType.NVarChar, o.MakerName),
                        CreateParameter("@RRIName", SqlDbType.NVarChar, o.RRIName),
                        CreateParameter("@Price", SqlDbType.NVarChar, o.Price),
                        CreateParameter("@FrequencyID", SqlDbType.NVarChar, o.FrequencyID),
                        CreateParameter("@ToolGroupID", SqlDbType.NVarChar, o.ToolGroupID),
                        CreateParameter("@SafeQty", SqlDbType.NVarChar, o.SafeQty),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.QTool.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(QTool o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@QToolID", SqlDbType.NVarChar, o.QToolID),
                        CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                        CreateParameter("@MakerName", SqlDbType.NVarChar, o.MakerName),
                        CreateParameter("@RRIName", SqlDbType.NVarChar, o.RRIName),
                        CreateParameter("@Price", SqlDbType.NVarChar, o.Price),
                        CreateParameter("@FrequencyID", SqlDbType.NVarChar, o.FrequencyID),
                        CreateParameter("@ToolGroupID", SqlDbType.NVarChar, o.ToolGroupID),
                        CreateParameter("@SafeQty", SqlDbType.NVarChar, o.SafeQty),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.QTool.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(QTool o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@QToolID", SqlDbType.NVarChar, o.QToolID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.QTool.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_QTool");

			return table;
		}

		public static DataTable GetAllLeftJoin()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.QTool.GetAllLeftJoin);
		}

		public static DataTable GetOneLeftJoin(string qtoolId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@QToolID", SqlDbType.NVarChar, qtoolId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.QTool.GetOneLeftJoin, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		#endregion
	}
}
